package com.zyd.dao;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.Map;

/**
 * @author: Wangbo
 * @date: 2019/3/12
 */
@Repository
public class SafeOrderDao {

    private static final Logger LOGGER = LoggerFactory.getLogger(SafeOrderDao.class);

    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 向订单表插入一条数据
     *
     * @param id        订单号
     * @param vehicleId 对应车辆的id
     * @param safeId    对应套餐的id
     * @param money     单位为分的金额
     */
    public void inster(String id, String vehicleId, String safeId, String money) {
        jdbcTemplate.update("INSERT INTO zydzn_safe_order (`id`,vehicle_id,safe_id,money,create_time) VALUES (?,?,?,?,?);", id, vehicleId, safeId, money, new Date());
    }

    public Map findByOrderId(String orderId) {
        Map resultMap = null;
        try {
            resultMap = jdbcTemplate.queryForMap("SELECT `id`,vehicle_id, safe_id,money ,create_time FROM zydzn_safe_order WHERE `id`= ? and `status`= 0 LIMIT 1", orderId);
        } catch (DataAccessException e) {
            LOGGER.info("跟据订单号id=" + orderId + "未查询到未支付的订单");
        }
        return resultMap;
    }

    /**
     * 订单交易成功
     *
     * @param id 订单号
     * @param timeEnd 交易结束时间
     */
    public void updateStatusById(Integer status ,String id, Date timeEnd) {
        jdbcTemplate.update("UPDATE zydzn_safe_order SET `status`= ? ,time_end = ? WHERE `id`= ? ",status, timeEnd, id);
    }
}
